import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sweetim/controller/chat_controller.dart';
import 'package:sweetim/controller/chat_record_controller.dart';
import 'package:sweetim/model/friends.dart';
import 'package:sweetim/view/chat_details_view.dart';

import '../model/chat_record.dart';

class ChatList extends StatelessWidget {
  const ChatList({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ChatRecordController chatRecordController = Get.put(ChatRecordController());

    return Center(
      child: Obx(() {
        if (chatRecordController.loading.value) {
          return const CircularProgressIndicator();
        }
        return ListView.separated(
          separatorBuilder: (ctx, index) => const Divider(),
          key: const PageStorageKey("messageList"),
          itemBuilder: (ctx, index) {
            var record = chatRecordController.list[index];
            return ChatItem(
              record,
              onTap: () {
                Get.to(() =>
                    ChatDetails(
                      friends: Friends(id: record.fid, name: record.friendName),
                    ))?.then((value) {
                  if (value != null) {
                    ChatRecord chatRecord = ChatRecord(id: record.id,
                        ctime: record.ctime,
                        content: value.toString(),
                        fid: record.fid,
                        friendName: record.friendName);
                    chatRecordController.list[index] = chatRecord;
                  }
                });
              },
              key: ValueKey(record.id!),
            );
          },
          itemCount: chatRecordController.list.length,
        );
      }),
    );
  }
}

class ChatItem extends StatelessWidget {
  final ChatRecord record;
  final VoidCallback onTap;

  const ChatItem(this.record, {required this.onTap, Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Container(
        margin: const EdgeInsets.fromLTRB(0, 2, 0, 0),
        child: SizedBox(
          height: 80,
          child: Row(
            children: [
              Expanded(
                flex: 2,
                child: CircleAvatar(
                  backgroundColor: Colors.indigo[200],
                  radius: 28,
                  child: Text(
                    record.friendName?.substring(0, 1) ?? "",
                    style: const TextStyle(
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              Expanded(
                flex: 7,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      record.friendName ?? "",
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: const TextStyle(fontSize: 16),
                    ),
                    const SizedBox(height: 8),
                    Text(
                      record.content ?? "",
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style:
                      const TextStyle(fontSize: 14, color: Colors.black54),
                    )
                  ],
                ),
              ),
              Expanded(
                flex: 1,
                child: CircleAvatar(
                  backgroundColor: Colors.red[200],
                  radius: 4,
                  child: Container(),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
